// lib/utils/bottom_sheet.dart

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

class LoadImageUtils {
  /// 显示选择图片的底部弹窗
  static void showImagePickerBottomSheet({
    required BuildContext context,
    required Future<void> Function(String path, BuildContext dialogContext) onImageSelected,
    String title = '',
    int limit = 1,
  }) {
    showModalBottomSheet(
      context: context,
      builder: (dialogContext) {
        return Container(
          padding: EdgeInsets.all(16),
          height: 200,
          child: Column(
            children: [
              Text(title),
              SizedBox(height: 16),
              ListTile(
                leading: Icon(Icons.camera_alt),
                title: Text('拍照'),
                onTap: () async {
                  final XFile? image = await ImagePicker().pickImage(
                    source: ImageSource.camera,
                  );
                  if (image != null && image.path.isNotEmpty) {
                    // 传递context以便上传完成后关闭弹窗
                    await onImageSelected(image.path, dialogContext);
                  }
                },
              ),
              ListTile(
                leading: Icon(Icons.photo),
                title: Text('相册'),
                onTap: () async {
                  // 上传多张图片
                  if(limit > 1) {
                    final List<XFile>? images = await ImagePicker().pickMultiImage(
                      limit: limit,
                    );
                    if (images != null && images.isNotEmpty) {
                      for (var image in images) {
                        // 传递context以便上传完成后关闭弹窗
                        await onImageSelected(image.path, dialogContext);
                      }
                    }
                  } else {
                    final XFile? image = await ImagePicker().pickImage(
                      source: ImageSource.gallery,
                      imageQuality: 98
                    );
                    if (image != null && image.path.isNotEmpty) {
                      // 传递context以便上传完成后关闭弹窗
                      await onImageSelected(image.path, dialogContext);
                    }
                  }
                },
              ),
            ],
          ),
        );
      },
    );
  }
}